package org.javacore.io.serializable;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import java.io.*;

/**
 * 描述:Java序列化和反序列化的小例子 Created by 子木 on 2016/2/15.
 */
public class SerializableT {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		for (int i = 0; i < 10; i++) {
			AObjcet aObjcet = new AObjcet();
			long beginTime = System.currentTimeMillis();

			ByteOutputStream byteOutput = new ByteOutputStream();
			ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutput);
			objectOutput.writeObject(aObjcet);
			objectOutput.close();
			byteOutput.close();
			byte[] bytes = byteOutput.toByteArray();
			System.out.println("Java序列化耗时：" + (System.currentTimeMillis() - beginTime) + "ms");
			System.out.println("Java序列化后的字节大小为：" + bytes.length);

			beginTime = System.currentTimeMillis();
			ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes);
			ObjectInputStream objectInput = new ObjectInputStream(byteInput);
			objectInput.readObject();
			objectInput.close();
			byteInput.close();
			System.out.println("Java反序列化耗时：" + (System.currentTimeMillis() - beginTime) + "ms");

		}
	}
}

@SuppressWarnings("serial")
class AObjcet implements Serializable {
	public String a = "bysocket";
	public String b = "likes";
	public String c = "java";
	public String d = "world";

	public int i = 100;
	public int j = 10;
	public long m = 100L;

	public boolean isA = true;
	public boolean isB = false;
	public boolean isC = false;

	public BObject aObject = new BObject();
	public BObject bObject = new BObject();
	public BObject cObject = new BObject();
	public BObject dObject = new BObject();

}

@SuppressWarnings("serial")
class BObject implements Serializable {

}
